package com.yl.chainStore.entity.pay

import com.baomidou.mybatisplus.annotation.IdType
import com.baomidou.mybatisplus.annotation.TableField
import com.baomidou.mybatisplus.annotation.TableId
import com.baomidou.mybatisplus.annotation.TableName
import com.yl.chainStore.common.Constants.OtherConfig.Companion.MONEY_TO_DB_DOUBLE
import com.yl.chainStore.common.enums.TypeEnum
import com.yl.common.annotation.NoArg
import com.yl.common.getContext
import java.math.BigDecimal
import java.math.RoundingMode

/**
 * <p>
 *
 * </p>
 *
 * @author cxw
 * @since 2021-07-05
 */
@NoArg
@TableName("sys_company_pay_item")
data class SysCompanyPayItem(

	/**
	 * id
	 */
	@TableId(value = "id", type = IdType.AUTO)
	var id: Long,

	/**
	 * 项目名称
	 */
	@TableField("item_name")
	var itemName: String,

	@TableField(exist = false)
	var nickname: String? = null,

	@TableField(exist = false)
	var showLabel: String? = null,

	/**
	 * 备注
	 */
	@TableField("remark")
	var remark: String? = null,

	/**
	 * 数据有效性
	 */
	@TableField("enabled")
	var enabled: Int = 1,

	/**
	 * 规格
	 */
	@TableField("specification")
	var specification: String? = null,

	/**
	 * 其他参数
	 */
	@TableField("other")
	var other: String? = null,

	/**
	 * 分类id
	 */
	@TableField("type_id")
	var typeId: Long,

	@TableField(exist = false)
	var typeName: String? = null,

	/**
	 * 单位
	 */
	@TableField("unit")
	var unit: String? = null,

	/**
	 * 品牌
	 */
	@TableField("brand")
	var brand: String? = null,

	/**
	 * 为空时是默认的，系统设置的
	 */
	@TableField("dept_id")
	var deptId: Long? = null,

	/**
	 * 推荐价格
	 */
	@TableField("recommend_price")
	var recommendPrice: Long = 1,


	/**
	 * 部门查询返回
	 */
	@TableField(exist = false)
	var deptRecommendPrice: Long? = null,

	/**
	 * 类型（商品，服务，耗材）
	 */
	@TableField("type")
	var type: TypeEnum = TypeEnum.OTHER,

	/**
	 * 收费项目配置
	 */
	@TableField(exist = false)
	var operatorConfig: List<SysCompanyPayItemsOperatorConfig>? = null,

	/**
	 * 收费项目回访配置
	 */
	@TableField(exist = false)
	var visitConfig: List<SysCompanyReturnVisitConfig>? = null,

	@TableField(exist = false)
	var deptItem: PayCompanyDeptItem? = null,

	/**
	 * 部门查询返回实际要显示的金额
	 */
	@TableField(exist = false)
	var deptRealPrice: BigDecimal? = null,


	/**
	 * 预警数量
	 */
	@TableField(exist = false)
	var warningCount: Int? = null,


	/**
	 * 是否高价
	 */
	@TableField(exist = false)
	var highPrice: Int = 0,


	/**
	 * 是否预警
	 */
	@TableField(exist = false)
	var warningStatus: Int = 0,

	/**
	 * 数量
	 */
	@TableField(exist = false)
	var count: Int = 0,

	/**
	 * 金额
	 */
	@TableField(exist = false)
	var warehouseSumMoney:Long = 0,

	/**
	 * 图片
	 */
	@TableField("image_url")
	var imageUrl:String? = null,

	/**
	 * 药准字
	 */
	@TableField("drug_approved")
	var drugApproved:String?=null,

	/**
	 * 保质期（天）
	 */
	@TableField("shelf_life_day")
	var shelfLifeDay:Int?= null
	) {

	/**
	 * 实际要显示的金额 推荐单价
	 */
	@TableField(exist = false)
	var realPrice: BigDecimal? = null
		get() {
			if (field == null) {
				if(deptRecommendPrice != null){
					return BigDecimal(deptRecommendPrice!!.div(MONEY_TO_DB_DOUBLE)).setScale(2, RoundingMode.HALF_UP)
				}else{
					return BigDecimal(recommendPrice.div(MONEY_TO_DB_DOUBLE)).setScale(2, RoundingMode.HALF_UP)
				}
			}
			return field
		}
		set(value) {
			if (value != null) {
				recommendPrice = (value.multiply(BigDecimal(MONEY_TO_DB_DOUBLE))).toLong()
				field = value
			}
		}



	@TableField(exist = false)
	var showName: String? = null
		get() {
			if (!nickname.isNullOrEmpty()) {
				return nickname
			}
			return itemName
		}

	@TableField(exist = false)
	var showSumMoney: BigDecimal? = null
		get() {
			if (field == null ) {
				return BigDecimal(warehouseSumMoney.div(MONEY_TO_DB_DOUBLE)).setScale(2, RoundingMode.HALF_UP)
			}
			return field
		}

	@TableField(exist = false)
	var averagePrice:BigDecimal?=null
	get(){
		if (field == null ) {
			if( count != 0){
				return BigDecimal(warehouseSumMoney.div(count).div(MONEY_TO_DB_DOUBLE)).setScale(2, RoundingMode.HALF_UP)
			}
		}
		return field
	}


	val canUpdateAll:Boolean
	get(){
		return deptId != null && deptId == getContext().subCompanyId
	}
}
